<template>
  <div class="paySearch">
    <x-header>保养录入
      <a href="javascript:;" class="save" @click="saveAction(type)">保存</a>
		  <a href="javascript:;" class="submit" @click="submitAction">提交</a>
    </x-header>
    <div class="paySearchContent">
      <div class="inspection">
        <div class="information-item equipment-item inspection-item">
          <img  class="information-item-leftImg" src="../../assets/img/securityCheckImg/titledecoration.png" alt="">
          <div class="information-item-title">表具信息</div>
        </div>
        <div class="information-item">
          <div class="information-item-title">出场编号</div>
          <popup-picker class="information-item-input" :data="list1" v-model="value1" @on-change="onChange" placeholder="请选择"></popup-picker>
          <!-- <img  class="information-item-rightImg" src="../../assets/img/securityCheckImg/rightArrow.png" alt=""> -->
        </div>
        <div class="information-item">
          <div class="information-item-title">上次示数</div>
          <input class="information-item-input" type="text" maxlength="16" placeholder="请录入" value="890">
        </div>
        <div class="information-item">
          <div class="information-item-title">本次示数</div>
          <input class="information-item-input" v-model="MAC_READ" type="text" maxlength="16" placeholder="请录入" value="">
          <img class="information-item-icon" src="../../assets/img/securityCheckImg/sao.png" alt="">
        </div>
        <div class="information-item">
          <div class="information-item-title">上次维修日期</div>
          <input class="information-item-input" type="text" maxlength="16" placeholder="请录入" value="890">
        </div>
        <div class="information-item">
          <div class="information-item-title">上次维修内容</div>
          <input class="information-item-input" type="text" maxlength="16" placeholder="请录入" value="890">
        </div>
        <div class="information-item equipment-item inspection-item">
          <img  class="information-item-leftImg" src="../../assets/img/securityCheckImg/titledecoration.png" alt="">
          <div class="information-item-title">维修保养项目</div>
        </div>
        <div class="entryRecord-item">
          <div class="information-item-title entryRecord-title">故障描述</div>
          <textarea class="information-item-textarea" v-model="FAULT_DESC" name="" id="" placeholder="请录入"></textarea>
        </div>
        <div class="entryRecord-item">
          <div class="information-item-title entryRecord-title">故障原因</div>
          <textarea class="information-item-textarea" v-model="FAULT_REA" name="" id="" placeholder="请录入"></textarea>
        </div>
        <div class="information-item">
          <div class="information-item-title">使用明细</div>
          <div class="information-item-radio inspection-item-radio information-item-input">
            <div class="information-item-radio-y"  @click="bindRadio($event,'01')">
              <input type="radio" name="radio1" value="01">
              <img class="information-item-radio-icon" src="../../assets/img/securityCheckImg/unselected.png" alt="" v-if="radioShowY">
              <img class="information-item-radio-icon" src="../../assets/img/securityCheckImg/selected.png" alt="" v-else>
              <div class="information-item-radio-text">注油</div>
            </div>
            <div class="information-item-radio-n"  @click="bindRadio($event,'02')">
              <input type="radio" name="radio1" value="02">
              <img class="information-item-radio-icon" src="../../assets/img/securityCheckImg/unselected.png" alt="" v-if="radioShowN">
              <img class="information-item-radio-icon" src="../../assets/img/securityCheckImg/selected.png" alt="" v-else>
              <div class="information-item-radio-text">清洗</div>
            </div>
          </div>
        </div>
        <div class="information-item information-item-s" v-for="(item, index) in MainItemList" :key="index">
          <div style="width: 30%;"></div>
          <div class="information-item-title">{{item.CODE_NAME}}</div>
          <input class="information-item-input" v-model="item.INPUT_VALUE" type="text" maxlength="16" placeholder="请录入" value="890">
        </div>
        <div class="entryRecord-item">
          <div class="information-item-title entryRecord-title">维修内容</div>
          <textarea class="information-item-textarea" v-model="MAIN_CONTENT" name="" id="" placeholder="请录入"></textarea>
        </div>
        <div class="information-item">
          <div class="information-item-title">维修后示数</div>
          <input class="information-item-input" v-model="AFTER_MAIN_READ" type="text" maxlength="16" placeholder="请录入" value="">
          <img class="information-item-icon" src="../../assets/img/securityCheckImg/sao.png" alt="">
        </div>
        <div class="information-item equipment-item inspection-item">
          <img  class="information-item-leftImg" src="../../assets/img/securityCheckImg/titledecoration.png" alt="">
          <div class="information-item-title">防盗装置信息</div>
          <img class="information-item-icon inspection-item-icon" src="../../assets/img/securityCheckImg/add.png" alt="">
        </div>
        <div class="information-item inspection-popupPicker">
          <div class="popupPicker-item">
            <popup-picker class="popupPicker-item-popup" :data="list1" v-model="value1" @on-change="onChange" placeholder="请选择"></popup-picker>
            <!-- <img class="popupPicker-item-icon" src="../../assets/img/securityCheckImg/selectB.png" alt=""> -->
          </div>
          <input class="popupPicker-item-input" type="text" maxlength="16" placeholder="请录入" value="">
          <div class="popupPicker-item-image">
            <img class="popupPicker-item-saoIcon" src="../../assets/img/securityCheckImg/sao.png" alt="">
          <img class="popupPicker-item-delIcon" src="../../assets/img/securityCheckImg/deleteData.png" alt="">
          </div>
        </div>
        <div class="information-item inspection-popupPicker">
          <div class="popupPicker-item">
            <popup-picker class="popupPicker-item-popup" :data="list1" v-model="value1" @on-change="onChange" placeholder="请选择"></popup-picker>
            <!-- <img class="popupPicker-item-icon" src="../../assets/img/securityCheckImg/selectB.png" alt=""> -->
          </div>
          <input class="popupPicker-item-input" type="text" maxlength="16" placeholder="请录入" value="">
          <div class="popupPicker-item-image">
            <img class="popupPicker-item-saoIcon" src="../../assets/img/securityCheckImg/sao.png" alt="">
          <img class="popupPicker-item-delIcon" src="../../assets/img/securityCheckImg/deleteData.png" alt="">
          </div>
        </div>
        <div class="information-item inspection-popupPicker">
          <div class="popupPicker-item">
            <popup-picker class="popupPicker-item-popup" :data="list1" v-model="value1" @on-change="onChange" placeholder="请选择"></popup-picker>
            <!-- <img class="popupPicker-item-icon" src="../../assets/img/securityCheckImg/selectB.png" alt=""> -->
          </div>
          <input class="popupPicker-item-input" type="text" maxlength="16" placeholder="请录入" value="">
          <div class="popupPicker-item-image">
            <img class="popupPicker-item-saoIcon" src="../../assets/img/securityCheckImg/sao.png" alt="">
          <img class="popupPicker-item-delIcon" src="../../assets/img/securityCheckImg/deleteData.png" alt="">
          </div>
        </div>
        <div class="information-item inspection-popupPicker">
          <div class="popupPicker-item">
            <popup-picker class="popupPicker-item-popup" :data="list1" v-model="value1" @on-change="onChange" placeholder="请选择"></popup-picker>
            <!-- <img class="popupPicker-item-icon" src="../../assets/img/securityCheckImg/selectB.png" alt=""> -->
          </div>
          <input class="popupPicker-item-input" type="text" maxlength="16" placeholder="请录入" value="">
          <div class="popupPicker-item-image">
            <img class="popupPicker-item-saoIcon" src="../../assets/img/securityCheckImg/sao.png" alt="">
          <img class="popupPicker-item-delIcon" src="../../assets/img/securityCheckImg/deleteData.png" alt="">
          </div>
        </div>
        <div class="information-item inspection-popupPicker">
          <div class="popupPicker-item">
            <popup-picker class="popupPicker-item-popup" :data="list1" v-model="value1" @on-change="onChange" placeholder="请选择"></popup-picker>
            <!-- <img class="popupPicker-item-icon" src="../../assets/img/securityCheckImg/selectB.png" alt=""> -->
          </div>
          <input class="popupPicker-item-input" type="text" maxlength="16" placeholder="请录入" value="">
          <div class="popupPicker-item-image">
            <img class="popupPicker-item-saoIcon" src="../../assets/img/securityCheckImg/sao.png" alt="">
          <img class="popupPicker-item-delIcon" src="../../assets/img/securityCheckImg/deleteData.png" alt="">
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { XHeader, PopupPicker } from "vux";
// import maintenance from "@/components/maintenanceEntry/index";
import Vue from "vue";
import { getsn } from "@/utils/getsn";
import { UserInfo } from "@/utils/public";
import {
  sqlEntryInsert,
  sqlEntryUpdata,
  sqlEntryDelete,
  submiEntryTremoval
} from "@/utils/sqlEntry";
export default {
  data() {
    return {
      value1: ["居民"],
      title1: "手机机型",
      list1: [
        ["小米1", "iPhone1", "华为1", "情怀1", "三星1", "其他1", "不告诉你1"]
      ],
      radioShowY: true,
      radioShowN: true,
      M_CID: "",
      CONS_NO: "",
      APP_NO: "",
      MainItemList: [],
      MAC_READ: "", //本次示数
      AFTER_MAIN_READ: "", //维修后读数
      FAULT_DESC: "", //故障
      FAULT_REA: "", //故障原因
      MAIN_CONTENT: "", //维修内容
      ADD_OIL: "", //加油
      CLEAN_FILTER: "", //清洗
      type: 1,
      isExistence_type: 1
    };
  },
  components: {
    XHeader,
    PopupPicker
  },
  methods: {
    bindRadio(e, type) {
      if (type == "01") {
        Vue.set(this, "radioShowY", !this.radioShowY);
        console.log();
        if (!this.radioShowY) {
          Vue.set(this, "ADD_OIL", "01");
        } else {
          Vue.set(this, "ADD_OIL", "");
        }
      }
      if (type == "02") {
        Vue.set(this, "radioShowN", !this.radioShowN);
        if (!this.radioShowN) {
          Vue.set(this, "CLEAN_FILTER", "01");
        } else {
          Vue.set(this, "CLEAN_FILTER", "");
        }
      }
      console.log('jiayou',this.ADD_OIL);
      console.log('qingxi',this.CLEAN_FILTER);
    },
    onChange(val) {
      console.log("val change", val);
    },
    saveAction(type) {
      console.log("保存");
      const _this = this;
      let M_CID = "";
      Vue.set(this, "type", type);
      if (this.isExistence_type == 2) {
        M_CID = this.M_CID;
      } else {
        M_CID = getsn();
      }
      console.log(this.CLEAN_FILTER, this.ADD_OIL);
      const mainItemData = this.MainItemList.map(item => {
        return {
          DEV_MAIN_DTL_ID: "",
          DEV_MAIN_ID: "",
          ORG_NO: UserInfo.ORG_NO,
          MAIN_ITEM: item.CODE_VALUE,
          MAIN_RST: item.INPUT_VALUE,
          M_CID: M_CID
        };
      });
      const data = [
        {
          CONS_NO: this.CONS_NO,
          DEV_MAIN_ID: "",
          ORG_NO: UserInfo.ORG_NO,
          CHANGE_TYPE: type == 2 ? "3" : this.isExistence_type == 2 ? "2" : "1",
          APP_NO: this.APP_NO,
          CONS_ID: "",
          GM_ID: "",
          ARRI_SITE_TIME: "",
          MAIN_FNS_TIME: "",
          WRITE_TIME: "",
          MAIN_ORG: "",
          MAIN_DEPT: "",
          MAIN_PER: "",
          CHK_RUSLT: "",
          MAC_READ: this.MAC_READ,
          AFTER_MAIN_READ: this.AFTER_MAIN_READ,
          ADD_OIL: this.ADD_OIL,
          CLEAN_FILTER: this.CLEAN_FILTER,
          HOME_SITU: "",
          WORK_RST: "",
          M_CID: M_CID,
          FAULT_DESC: this.FAULT_DESC,
          FAULT_REA: this.FAULT_REA,
          MAIN_CONTENT: this.MAIN_CONTENT
        }
      ];
      if (this.isExistence_type == 2) {
        sqlEntryUpdata("MIAN_RST_INFO", M_CID, data, this.callback);
        sqlEntryDelete("MIAN_DTL_INFO", M_CID, function(str, msg) {
          console.log(str, msg);
        });
        sqlEntryInsert("MIAN_DTL_INFO", mainItemData, function(str, msg) {
          _this.$vux.toast.text("修改成功");
        });
      } else {
        sqlEntryInsert("MIAN_RST_INFO", data, this.callback);
        sqlEntryInsert("MIAN_DTL_INFO", mainItemData, this.callback);
      }
    },
    submitAction() {
      console.log("提交");
      const mainItemData = this.MainItemList.map(item => {
        return {
          DEV_MAIN_DTL_ID: "",
          DEV_MAIN_ID: "",
          ORG_NO: UserInfo.ORG_NO,
          MAIN_ITEM: item.CODE_VALUE,
          MAIN_RST: item.INPUT_VALUE,
          M_CID: M_CID
        };
      });
      const data = [
        {
          CONS_NO: this.CONS_NO,
          DEV_MAIN_ID: "",
          ORG_NO: UserInfo.ORG_NO,
          CHANGE_TYPE: 3,
          APP_NO: this.APP_NO,
          CONS_ID: "",
          GM_ID: "",
          ARRI_SITE_TIME: "",
          MAIN_FNS_TIME: "",
          WRITE_TIME: "",
          MAIN_ORG: "",
          MAIN_DEPT: "",
          MAIN_PER: "",
          CHK_RUSLT: "",
          MAC_READ: this.MAC_READ,
          AFTER_MAIN_READ: this.AFTER_MAIN_READ,
          ADD_OIL: this.ADD_OIL,
          CLEAN_FILTER: this.CLEAN_FILTER,
          HOME_SITU: "",
          WORK_RST: "",
          M_CID: "",
          FAULT_DESC: this.FAULT_DESC,
          FAULT_REA: this.FAULT_REA,
          MAIN_CONTENT: this.MAIN_CONTENT,
          MIAN_DTL_INFO: mainItemData
        }
      ];
      submiEntryTremoval(data, "RMD043", this.submitCallback);
    },
    callback(str, msg) {
      console.log(str, msg);
      if (msg.resultCode == "9999") {
        if (this.type == 2) {
          this.$vux.toast.text("提交成功");
        } else {
          if (str == "sqlUpdata") {
            this.$vux.toast.text("修改成功");
          } else {
            this.$vux.toast.text("添加成功");
          }
        }
      }
    },
    submitCallback(result) {
      console.log(result);
      this.saveAction(2);
    },
    getMIAN_DTL_INFO(m_cid) {
      console.log(12312312312, m_cid);
      const _this = this;
      AndroidApi.queryTableData(
        JSON.stringify({
          whereClause: "select * from MIAN_DTL_INFO where M_CID = ?",
          whereArgs: [m_cid]
        }),
        function(msg) {
          console.log(2222222222222222, msg);
          if (msg.resultCode === "9999") {
            if (msg.result.length > 0) {
              const data = msg.result.map(item => {
                for (let i in _this.MainItemList) {
                  if (_this.MainItemList[i].CODE_VALUE == item.MAIN_ITEM) {
                    item.MAIN_ITEM_NAME = _this.MainItemList[i].CODE_NAME;
                  }
                }
                return {
                  CODE_NAME: item.MAIN_ITEM_NAME,
                  CODE_VALUE: item.MAIN_ITEM,
                  INPUT_VALUE: item.MAIN_RST
                };
              });
              Vue.set(_this, "MainItemList", data);
            }
          }
        }
      );
    }
  },
  mounted() {
    const _this = this; // mdmMainItem
    Vue.set(this, "CONS_NO", localStorage.getItem("CONS_NO"));
    Vue.set(this, "APP_NO", localStorage.getItem("APP_NO"));
    // 使用明细
    AndroidApi.queryTableData(
      '{"whereClause":"select * from P_CODE where code_type= ?","whereArgs":["mdmMainItem"]}',
      function(msg) {
        if (msg.resultCode === "9999") {
          if (msg.result.length != 0) {
            const MainItemList = msg.result.map(item => {
              return {
                CODE_NAME: item.CODE_NAME,
                CODE_VALUE: item.CODE_VALUE,
                INPUT_VALUE: ""
              };
            });
            Vue.set(_this, "MainItemList", MainItemList);
          } else {
            Vue.set(_this, "MainItemList", []);
          }
        }
      }
    );
    // 查询表是否存在数据
    AndroidApi.queryTableData(
      JSON.stringify({
        whereClause:
          "select * from MIAN_RST_INFO where CONS_NO= ? AND CHANGE_TYPE IN (1,2)",
        whereArgs: [_this.CONS_NO]
      }),
      function(msg) {
        console.log(111111111111111111111111, msg);
        if (msg.resultCode === "9999") {
          if (msg.result.length > 0) {
            _this.getMIAN_DTL_INFO(msg.result[0].M_CID);
            Vue.set(_this, "M_CID", msg.result[0].M_CID);
            Vue.set(_this, "MAC_READ", msg.result[0].MAC_READ);
            Vue.set(_this, "AFTER_MAIN_READ", msg.result[0].AFTER_MAIN_READ);
            Vue.set(_this, "FAULT_DESC", msg.result[0].FAULT_DESC);
            Vue.set(_this, "FAULT_REA", msg.result[0].FAULT_REA);
            Vue.set(_this, "MAIN_CONTENT", msg.result[0].MAIN_CONTENT);
            Vue.set(_this, "ADD_OIL", msg.result[0].ADD_OIL);
            Vue.set(_this, "CLEAN_FILTER", msg.result[0].CLEAN_FILTER);
            if (msg.result[0].ADD_OIL == "01") {
              Vue.set(_this, "radioShowY", false);
            } else {
              Vue.set(_this, "radioShowY", true);
            }
            if (msg.result[0].CLEAN_FILTER == "01") {
              Vue.set(_this, "radioShowN", false);
            } else {
              Vue.set(_this, "radioShowN", true);
            }
            Vue.set(_this, "isExistence_type", 2);
          } else {
            Vue.set(_this, "isExistence_type", 1);
          }
        }
      }
    );
  }
};
</script>
<style scoped lang="less">
@import "../../assets/less/style.less";
</style>
<style lang="less">
@import "../../assets/less/vuxStyle.less";
.popupPicker-item-popup {
  .vux-popup-picker-select {
    text-align: center !important;
    margin-left: -0.2778rem;
  }
}
</style>
